import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
import java.util.*;

public class Filter 
{
	public static HashSet<String> enableObjExtension = null;
	
	Filter()
	{
		enableObjExtension = new HashSet<String>();
		
		enableObjExtension.add("o");
		enableObjExtension.add("jar");
	}
	
	private void filter(String inGdfFile, String outGdfFile) throws IOException
	{
		//in buffer
		FileInputStream infstream = new FileInputStream(inGdfFile);
		DataInputStream inStream = new DataInputStream(infstream);
		BufferedReader inBuffer = new BufferedReader(new InputStreamReader(inStream));
		
		//out buffer
		FileWriter outfstream = new FileWriter(outGdfFile);
		BufferedWriter outBuffer = new BufferedWriter(outfstream);
		
	  
		String line = null;
		//bypass the node-def header
	  	line = inBuffer.readLine();
	  	outBuffer.write(line + "\n");
	  
	  	//int tt = 0;
	  	//int ss = 0;
	  	
	  	HashSet<String> enableObjSet = new HashSet<String>();
	  	
	  	while((line = inBuffer.readLine()) != null)
	  	{
	  		String words[] = line.split(",");
	  		
	  		//get out of the loop when going into the edge-def region
	  		if (words[0].equals("edgedef> node1"))
	  		{
	  		  	outBuffer.write(line + "\n");
	  		  	
	  			break;
	  		}
	  		
	  		if(enableObjExtension.contains(words[5]) == true)
	  		{
	  		  	outBuffer.write(line + "\n");
	  			
	  			enableObjSet.add(words[0]);
	  		}
	  	}
	  	
		while((line = inBuffer.readLine()) != null)
		{	
			String words[] = line.split(",");
			
			if (enableObjSet.contains(words[0]) && enableObjSet.contains(words[1]))
			{
			  	outBuffer.write(line + "\n");
			}
		}
		
		inBuffer.close();
		outBuffer.close();
	}
	
	public static void main(String args[]) throws IOException
	{
		Filter fuyun = new Filter();
		fuyun.filter("data/android-current/droid.gdf", "data/android-current/filteredDroid.gdf");
	}
}
